package com.cvte.boe.media.views.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import com.cvte.boe.media.R;
import com.cvte.boe.media.model.LrcRow;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuguitong on 2018/5/22.
 */
public class LrcView extends View{
    List<LrcRow> lrcRowList;
    private Context mContext;
    private String mLoadingLrcTip;
    private int mHighLightRowColor = Color.RED;
    private int mNormalRowColor = Color.BLUE;
    private float mLrcFontSize;
    private float mHighLightLrcFontSize = 0;
    private int mHignlightRow = 0;   // current singing row , should be highlighted.
    private int mPaddingY = 10;		// padding of each row
    private int mOffsetY = 0;
    public LrcView(Context context) {
        this(context,null);
    }

    public LrcView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LrcView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        lrcRowList = new ArrayList<LrcRow>();
        mLoadingLrcTip = context.getString(R.string.on_found_lrc);
        mHighLightRowColor = mContext.getResources().getColor(R.color.lrc_view_high_light_color);
        mNormalRowColor = mContext.getResources().getColor(R.color.lrc_view_normal_color);
        mLrcFontSize = mContext.getResources().getDimension(R.dimen.lrc_view_normal_text_size);
        mHighLightLrcFontSize = mContext.getResources().getDimension(R.dimen.lrc_view_high_light_text_size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        final int height = getHeight(); // height of this view
        final int width = getWidth(); // width of this view

        Paint curPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        LinearGradient curLinearGradient = new LinearGradient(0,0,0,height,new int[]{mHighLightRowColor, mHighLightRowColor},null,LinearGradient.TileMode.CLAMP);
        curPaint.setShader(curLinearGradient);
        curPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY));
        if (lrcRowList == null || lrcRowList.size() == 0) {
            if (mLoadingLrcTip != null) {
                // draw tip when no lrc.
                curPaint.setTextSize(mHighLightLrcFontSize);
                curPaint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(mLoadingLrcTip, width / 2, height / 2, curPaint);
            }
            return;
        }

        int rowY = 0; // vertical point of each row.
        final int rowX = width / 2;
        int rowNum = 0;

        String highlightText = lrcRowList.get(mHignlightRow).getContent();
        int highlightRowY = (int)(height / 2) + mOffsetY;
        curPaint.setTextSize(mHighLightLrcFontSize);
        curPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(highlightText, rowX, highlightRowY, curPaint);

        Paint upPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        upPaint.setTextSize(mLrcFontSize);
        upPaint.setTextAlign(Paint.Align.CENTER);
        rowNum = mHignlightRow - 1;
        rowY = (int)(highlightRowY - mPaddingY - mHighLightLrcFontSize);
        LinearGradient upLinearGradient = new LinearGradient(0,0,0,rowY,new int[]{0x00ffffff&mNormalRowColor,mNormalRowColor},null,LinearGradient.TileMode.CLAMP);
        upPaint.setShader(upLinearGradient);
        upPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY));

        while( rowY > -mLrcFontSize && rowNum >= 0){
            String text = lrcRowList.get(rowNum).getContent();
            canvas.drawText(text, rowX, rowY, upPaint);
            rowY -=  (mPaddingY + mLrcFontSize);
            rowNum --;
        }
        Paint downPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        downPaint.setTextSize(mLrcFontSize);
        downPaint.setTextAlign(Paint.Align.CENTER);
        rowNum = mHignlightRow + 1;
        rowY = (int)(highlightRowY + mPaddingY + mHighLightLrcFontSize);
        LinearGradient doenLinearGradient = new LinearGradient(0,rowY,0,height,new int[]{mNormalRowColor,0x00ffffff&mNormalRowColor},null,LinearGradient.TileMode.CLAMP);
        downPaint.setShader(doenLinearGradient);
        downPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY));

        while( rowY < height && rowNum < lrcRowList.size()){
            String text = lrcRowList.get(rowNum).getContent();
            canvas.drawText(text, rowX, rowY, downPaint);
            rowY += (mPaddingY + mLrcFontSize);
            rowNum ++;
        }
    }

    public void setLrcRowList(List<LrcRow> lrcRowList) {
        this.lrcRowList.clear();
        if(lrcRowList != null) {
            this.lrcRowList.addAll(lrcRowList);
        }
        mHignlightRow = 0;
        seekToLrcTime(0);
    }
    public void seekToLrcTime(long time){
        for(int i = 0;i < lrcRowList.size() ;i++){
            LrcRow row = lrcRowList.get(i);
            if(row.isContainTime(time)){
                mHignlightRow = i;
                if(row.getEndTime() != LrcRow.END_TIME && (row.getEndTime() - row.getStartTime()) > 0) {
                    mOffsetY = -(int) ((mHighLightLrcFontSize * (time - row.getStartTime())) / (row.getEndTime() - row.getStartTime()));
                }else{
                    mOffsetY = 0;
                }
                break;
            }
        }
        postInvalidate();
    }

}
